package com.chatroom.controller;

import com.chatroom.model.ChatMessage;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.stereotype.Controller;

@Controller
public class ChatController {
    @MessageMapping("/chat.sendMessage")
    @SendTo("/topic/public")
    public ChatMessage sendMessage(@Payload ChatMessage chatmessage){
        return chatmessage;
    }

    @MessageMapping("/chat.addUser")
    @SendTo("/topoc/public")
    public ChatMessage addUser(@Payload ChatMessage chatmessage, SimpMessageHeaderAccessor headerAccessor){
//        如果请求addUser就能匹配到
        headerAccessor.getSessionAttributes().put("username",chatmessage.getSender());
        return chatmessage;
    }
}
